package org.zasadnyy.redlions.players.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ScServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(ScServlet.class
			.getName());

	private Map<String, String> urlMapping = new HashMap<String, String>();
	{
		urlMapping.put("/playersApp", "sc_playersApp.zip");
		urlMapping.put("/editPlayersApp", "sc_editPlayersApp.zip");
		urlMapping.put("/editPlayersGwtApp", "sc_editPlayersGwtApp.zip");
	}

	/**
	 * This servlet receives all '/sc/*' requests according to a rule defined in
	 * 'web.xml'. It looks for the necessary resource in 'sc.zip' which should
	 * be present in the application classpath.
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String zipFileName = "";
		String requestedURI = "";
		String incomeUrl = req.getRequestURI();

		for (Map.Entry<String, String> entry : urlMapping.entrySet()) {
			if (incomeUrl.contains(entry.getKey())) {
				requestedURI = incomeUrl.replaceFirst(entry.getKey(), "");
				zipFileName = entry.getValue();
			}
		}

		assert "".equals(zipFileName);

		// Sometimes the request URI begins with '/sc' and other times with
		// '/myappname/sc', so we always remove '/myappname' from the request
		// URI
		// String requestedURI = req.getRequestURI()
		// .replaceFirst("/myappname", "");

		// At this point requestedURI begins with '/sc/...' but the zip file
		// entries
		// begin with 'sc/...', so we remove the first '/'
		requestedURI = requestedURI.substring(1);

		log.info("Requested URI '" + req.getRequestURI() + "' converted to '"
				+ requestedURI + "'");

		// search for resource
		File zipFile = new File(zipFileName);
		log.info("Try to acces to file: " + zipFile.getAbsolutePath());
		ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
		ZipEntry entry;
		while ((entry = in.getNextEntry()) != null) {

			if (requestedURI.equals(entry.getName())) {
				// resource found
				// redirect it to output stream
				OutputStream out = resp.getOutputStream();
				byte[] buf = new byte[1024];
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				in.close();
				out.close();
				log.info("Requested '" + requestedURI
						+ "' found in zip file entry: " + entry.getName());
				return;
			}
		}

		log.severe("Requested '" + requestedURI + "' not found in zip file!");
	}

}
